﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace Smartkernel.IO
{
    /// <summary>
    /// 智能序列化
    /// </summary>
    public static class SmartSerialize
    {
        /// <summary>
        /// 序列化过程
        /// </summary>
        /// <param name="obj">准备序列化的对象</param>
        /// <param name="stream">序列化对象写入的流</param>
        /// <param name="smartSerializeType">序列化的类型</param>
        public static void Serialize(Object obj, Stream stream, SmartSerializeType smartSerializeType)
        {
            IFormatter formatter = null;
            switch (smartSerializeType)
            {
                case SmartSerializeType.Soap:
                    formatter = new SoapFormatter();
                    break;
                case SmartSerializeType.Binary:
                    formatter = new BinaryFormatter();
                    break;
                default:
                    break;
            }
            formatter.Serialize(stream, obj);
            stream.Position = 0;
        }
        /// <summary>
        /// 反序列化过程
        /// </summary>
        /// <typeparam name="TTarget">反序列化的目标类型</typeparam>
        /// <param name="stream">序列化对象的流</param>
        /// <param name="smartSerializeType">序列化的类型</param>
        /// <returns>返回反序列化的对象</returns>
        public static TTarget Deserialize<TTarget>(Stream stream, SmartSerializeType smartSerializeType)
        {
            IFormatter formatter = null;
            switch (smartSerializeType)
            {
                case SmartSerializeType.Soap:
                    formatter = new SoapFormatter();
                    break;
                case SmartSerializeType.Binary:
                    formatter = new BinaryFormatter();
                    break;
                default:
                    break;
            }
            return (TTarget)formatter.Deserialize(stream);
        }
        /// <summary>
        /// 序列化过程
        /// </summary>
        /// <param name="obj">准备序列化的对象</param>
        /// <param name="path">序列化写入的文件</param>
        /// <param name="smartSerializeType">序列化的类型</param>
        public static void Serialize(Object obj, string path, SmartSerializeType smartSerializeType)
        {
            string directoryName = Path.GetDirectoryName(path);
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            using (Stream stream = new FileStream(path, FileMode.CreateNew))
            {
                Serialize(obj, stream, smartSerializeType);
            }
        }
        /// <summary>
        /// 反序列化过程
        /// </summary>
        /// <typeparam name="TTarget">反序列化的目标类型</typeparam>
        /// <param name="path">读取序列化数据的文件</param>
        /// <param name="smartSerializeType">序列化的类型</param>
        /// <returns>返回反序列化的对象</returns>
        public static TTarget Deserialize<TTarget>(string path, SmartSerializeType smartSerializeType)
        {
            using (Stream stream = new FileStream(path, FileMode.Open))
            {
                return Deserialize<TTarget>(stream, smartSerializeType);
            }
        }
    }
}
